﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;

namespace BladeTecWebApplication.Admin
{
	public partial class ViewApplicants : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				NetSysData.NetSysData data = new NetSysData.NetSysData();
				GetApplicants_Result[] applicants = data.GetApplicants();

				gvApplicants.DataSource = applicants.OrderBy(a => a.LAST_NAME);
				gvApplicants.DataBind();

				Session["sortField"] = "LAST_NAME";
				Session["sortDirection"] = "Ascending";

			}
		}

		protected void gvApplicants_Sorting(object sender, GridViewSortEventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			GetApplicants_Result[] applicants = data.GetApplicants();
			
			if (Session["sortDirection"].ToString() == "Ascending")
			{
				switch(e.SortExpression.ToString()){
					case "FIRST_NAME":
						gvApplicants.DataSource = applicants.OrderByDescending(a => a.FIRST_NAME);
						break;
					case "LAST_NAME":
						gvApplicants.DataSource = applicants.OrderByDescending(a => a.LAST_NAME);
						break;
					case "INTERVIEW_DATE":
						gvApplicants.DataSource = applicants.OrderByDescending(a => a.INTERVIEW_DATE);
						break;
					case "EMAIL":
						gvApplicants.DataSource = applicants.OrderByDescending(a => a.EMAIL);
						break;
					case "POSITION_TITLE":
						gvApplicants.DataSource = applicants.OrderByDescending(a => a.POSITION_TITLE);
						break;
				}
				
				Session["sortDirection"] = "Descending";
			}
			else
			{
				switch (e.SortExpression.ToString())
				{
					case "FIRST_NAME":
						gvApplicants.DataSource = applicants.OrderBy(a => a.FIRST_NAME);
						break;
					case "LAST_NAME":
						gvApplicants.DataSource = applicants.OrderBy(a => a.LAST_NAME);
						break;
					case "INTERVIEW_DATE":
						gvApplicants.DataSource = applicants.OrderBy(a => a.INTERVIEW_DATE);
						break;
					case "EMAIL":
						gvApplicants.DataSource = applicants.OrderBy(a => a.EMAIL);
						break;
					case "POSITION_TITLE":
						gvApplicants.DataSource = applicants.OrderBy(a => a.POSITION_TITLE);
						break;
				}
				Session["sortDirection"] = "Ascending";
			}

			Session["sortField"] = e.SortExpression.ToString();

			gvApplicants.DataBind();
		}

		protected void btnDelete_Click(object sender, EventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			foreach (string s in Request.Form)
			{
				if (s.IndexOf("applicant_") == 0)
				{
					int applicant_id = Int32.Parse(Request.Form.Get(s));
					data.DeleteApplicant(applicant_id);
				}
			}

			GetApplicants_Result[] applicants = data.GetApplicants();

			gvApplicants.DataSource = applicants.OrderBy(a => a.LAST_NAME);
			gvApplicants.DataBind();

		}
	}
}